﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class FightSystem : MonoBehaviour
{
    public static FightData fightData;
    public static MoveList moveList = new MoveList();
    public static Team playerTeam;
    public static Team enemyTeam;

    

    public static Character actor;
    public ActorState actorState = ActorState.Start;

    public enum ActorState
    {
        Start,Acting,End
    }



    // Start is called before the first frame update
    void Start()
    {
        fightData = GameObject.FindGameObjectWithTag("fightData").GetComponent<FightData>();
        playerTeam = fightData.playerTeam;
        enemyTeam = new Team(fightData.enemyTeamData);
        moveList.Reset();
        actor = moveList.GetActor();
    }

    // Update is called once per frame
    void Update()
    {
        actor.Action();
        CheckAlive();
        GetFightResult();
    }

    public static void NextActor()
    {
        moveList.Reset();
        actor = moveList.GetActor();
    }


    void CheckAlive()
    {
        foreach (Character character in playerTeam.characters)
        {
            if (character.currentHp <= 0) character.alive = false;
        }
        foreach (Character character in enemyTeam.characters)
        {
            if (character.currentHp <= 0) character.alive = false;
        }
    }

    void GetFightResult()
    {
        bool win = true;
        bool lose = true;
        foreach(Character character in enemyTeam.characters)
        {
            if (character.alive == true) win = false;
        }
        if (win)
        {
            Debug.Log("win");
            return;
        }
        foreach (Character character in playerTeam.characters)
        {
            if (character.alive == true) lose = false;
        }
        if (lose)
        {
            Debug.Log("win");
            return;
        }
    }

}
